package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.EnterpriseWeChat.entity.VipSkinInfo;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class queryUserSkinInfo extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public queryUserSkinInfo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public VipSkinInfo getData(String vipNoOrPh) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(vipNoOrPh), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");


        if(jsonArray.length()>0){
            VipSkinInfo vipSkinInfo = new VipSkinInfo();
            vipSkinInfo.setColor(jsonArray.getJSONArray(0).getString(0));
            vipSkinInfo.setType(jsonArray.getJSONArray(0).getString(1));
            vipSkinInfo.setQuality(jsonArray.getJSONArray(0).getString(2));
            vipSkinInfo.setShape(jsonArray.getJSONArray(0).getString(3));
            vipSkinInfo.setSummarize(jsonArray.getJSONArray(0).getString(4));
            return vipSkinInfo;
        }
        return null;
    }

    private MultiValueMap Data(String vipNoOrPh) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

//        colums.add("ID");
        colums.add("'APP_VIP_COLOR_ID;NAME'");//色
        colums.add("'APP_VIP_TYPE_ID;NAME'");//型
        colums.add("'APP_VIP_QUALITY_ID;NAME'");//质
        colums.add("'APP_VIP_SHAPE_ID;NAME'");//体型
        colums.add("APP_CTQ_DESCRIPTION");//总结

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名
        params.put("count","true");
//        HashMap orderby = new HashMap();
//        orderby.put("column","BILLDATE");
//        orderby.put("asc","false");
//
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//        params.put("orderby",list);

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        expr1.put("column", "'CARDNO'");
        expr1.put("condition", "'"+vipNoOrPh+"'");
        HashMap expr2 = new HashMap();
        expr2.put("column", "'MOBIL'");
        expr2.put("condition", "'"+vipNoOrPh+"'");

        paramss.put("combine", "or");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);

//        params.put("params", paramss);
//        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        queryUserSkinInfo g = new queryUserSkinInfo("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {//3125
            System.err.println(g.getData("13643111382"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
